package com.fastbee.sip.service.impl;

import com.fastbee.common.constant.FastBeeConstant;
import com.fastbee.common.core.domain.entity.SysUser;
import com.fastbee.common.core.redis.RedisCache;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.mapper.DeviceMapper;
import com.fastbee.sip.conf.SysSipConfig;
import com.fastbee.sip.domain.SipConfig;
import com.fastbee.sip.mapper.SipConfigMapper;
import com.fastbee.sip.service.ISipConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.fastbee.common.utils.SecurityUtils.getLoginUser;

/**
 * sip系统配置Service业务层处理
 *
 * @author zhuangpeng.li
 * @date 2022-11-30
 */
@Service
public class SipConfigServiceImpl implements ISipConfigService {
    @Autowired
    private SipConfigMapper sipConfigMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private SysSipConfig sysSipConfig;

    @Autowired
    private RedisCache redisCache;

    @Override
    public int updateDefaultSipConfig(SipConfig sipConfig) {
        redisCache.setCacheObject(FastBeeConstant.REDIS.DEFAULT_SIP_CONFIG,sipConfig);
        return 1;
    }

    @Override
    public SipConfig GetDefaultSipConfig() {
        SipConfig config =  redisCache.getCacheObject(FastBeeConstant.REDIS.DEFAULT_SIP_CONFIG);
        if (config != null) {
            List<SipConfig> sipConfiglist = this.selectSipConfigBytenantId(1L);
            if (sipConfiglist != null && sipConfiglist.size() > 0) {
                SipConfig sipConfig = sipConfiglist.get(0);
                redisCache.setCacheObject(FastBeeConstant.REDIS.DEFAULT_SIP_CONFIG,sipConfig);
                return sipConfig;
            }
        }
        return config;
    }

    /**
     * 查询产品下第一条sip系统配置
     *
     * @return sip系统配置
     */
    @Override
    public List<SipConfig> selectSipConfigByProductId(Long productId) {
        return sipConfigMapper.selectSipConfigByProductId(productId);
    }

    @Override
    public List<SipConfig> selectSipConfigBytenantId(Long tenantId) {
        return sipConfigMapper.selectSipConfigBytenantId(tenantId);
    }

    @Override
    public SipConfig selectSipConfigBydeviceSipId(String deviceSipId) {
        Device device = deviceMapper.selectDeviceBySerialNumber(deviceSipId);
        if (device != null) {
            return sipConfigMapper.selectSipConfigByproductId(device.getProductId());
        } else {
            return this.GetDefaultSipConfig();
        }
    }

    /**
     * 查询sip系统配置列表
     *
     * @param sipConfig sip系统配置
     * @return sip系统配置
     */
    @Override
    public List<SipConfig> selectSipConfigList(SipConfig sipConfig) {
        return sipConfigMapper.selectSipConfigList(sipConfig);
    }

    /**
     * 新增sip系统配置
     *
     * @param sipConfig sip系统配置
     * @return 结果
     */
    @Override
    public int insertSipConfig(SipConfig sipConfig) {
        List<SipConfig> list = selectSipConfigByProductId(sipConfig.getProductId());
        if (list != null && list.size() > 0) {
            updateSipConfig(sipConfig);
            return 0;
        } else {
            sipConfig.setCreateTime(DateUtils.getNowDate());
            sipConfig.setDomain(sysSipConfig.getDomain());
            sipConfig.setServerSipid(sysSipConfig.getId());
            SysUser user = getLoginUser().getUser();
            sipConfig.setTenantId(user.getUserId());
            sipConfig.setTenantName(user.getUserName());
            return sipConfigMapper.insertSipConfig(sipConfig);
        }
    }

    /**
     * 修改sip系统配置
     *
     * @param sipConfig sip系统配置
     * @return 结果
     */
    @Override
    public int updateSipConfig(SipConfig sipConfig) {
        if (sipConfig.getIsdefault() == 1) {
            sipConfigMapper.updatedefaultSipConfig(0);
        }
        sipConfig.setUpdateTime(DateUtils.getNowDate());
        sipConfig.setDomain(sysSipConfig.getDomain());
        sipConfig.setServerSipid(sysSipConfig.getId());
        this.updateDefaultSipConfig(sipConfig);
        return sipConfigMapper.updateSipConfig(sipConfig);
    }

    @Override
    public void syncSipConfig(SysSipConfig sipConfig) {
        List<SipConfig> list = sipConfigMapper.selectSipConfigList(new SipConfig());
        for (SipConfig config : list) {
            config.setDomain(sipConfig.getDomain());
            config.setServerSipid(sipConfig.getId());
            config.setIp(sipConfig.getIp());
            config.setPort(sipConfig.getPort());
            sipConfigMapper.updateSipConfig(config);
        }
    }

    /**
     * 批量删除sip系统配置
     *
     * @param ids 需要删除的sip系统配置主键
     * @return 结果
     */
    @Override
    public int deleteSipConfigByIds(Long[] ids) {
        return sipConfigMapper.deleteSipConfigByIds(ids);
    }

    @Override
    public int deleteSipConfigByProductIds(Long[] productIds) {
        return sipConfigMapper.deleteSipConfigByProductId(productIds);
    }

    /**
     * 删除sip系统配置信息
     *
     * @param id sip系统配置主键
     * @return 结果
     */
    @Override
    public int deleteSipConfigById(Long id) {
        return sipConfigMapper.deleteSipConfigById(id);
    }
}
